package com.study;

import java.time.LocalDate;
import java.util.Scanner;

public class Operate {
    private Dvd[] dvds;


    public Operate() {
        dvds = new Dvd[] {
                new Dvd("肖申克的救赎", "剧情", true, false, 2, 3, 10),
                new Dvd("这个杀手不太冷", "动作", true, false, 3, 4, 15),
                new Dvd("阿甘正传", "剧情", true, false, 1, 2, 8),
                new Dvd("霸王别姬", "剧情", true, false, 2, 3, 10),
                new Dvd("盗梦空间", "科幻", true, false, 3, 4, 15)
        };
    }



    // 借出排行榜

    public void rentalRank() {
        if (dvds.length == 0) {
            System.out.println("DVD库中还没有DVD。\n");
            return;
        }

        System.out.println("借出排行榜：");
        for (int i = 0; i < dvds.length; i++) {
            System.out.println((i + 1) + ". " + dvds[i].getTitle());
        }
        System.out.println();
    }


    // 新增DVD

    public void addDvd() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入DVD标题：");
        String title = scanner.nextLine();

        System.out.print("请输入DVD类型：");
        String genre = scanner.nextLine();

        System.out.print("请输入DVD租金：");
        int rentalFee = scanner.nextInt();

        System.out.print("请输入DVD补费：");
        int lateFee = scanner.nextInt();

        System.out.print("请输入DVD押金：");
        int deposit = scanner.nextInt();

        Dvd newDvd = new Dvd(title, genre, true, false, rentalFee, lateFee, deposit);
        Dvd[] newDvds = new Dvd[dvds.length + 1];
        for (int i = 0; i < dvds.length; i++) {
            newDvds[i] = dvds[i];
        }
        newDvds[dvds.length] = newDvd;
        dvds = newDvds;

        System.out.println("成功添加DVD《" + title + "》。\n");
    }


    // 查看DVD
    public void checkDvd() {
        if (dvds.length == 0) {
            System.out.println("DVD库中还没有DVD。\n");
            return;
        }

        System.out.println("序号\t状态\t\t名称\t\t借出日期");
        for (int i = 0; i < dvds.length; i++) {
            Dvd dvd = dvds[i];
            String status = dvd.isAvailable() ? "可借" : "已借出";
            String rentedDate = dvd.getRentedDate() != null ? dvd.getRentedDate().toString() : "-";
            System.out.println((i + 1) + "\t" + status + "\t" + dvd.getTitle() + "\t" + rentedDate);
        }
        System.out.println();
    }



    // 删除DVD
    public void deleteDvd() {
        if (dvds.length == 0) {
            System.out.println("DVD库中还没有DVD。\n");
            return;
        }

        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入DVD名称：");
        String name = scanner.nextLine();

        boolean flag = false;
        Dvd[] newDvds = new Dvd[dvds.length - 1];
        int j = 0;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i].getTitle().equals(name)) {
                if (!dvds[i].isAvailable()) {
                    System.out.println("DVD《" + dvds[i].getTitle() + "》已经被借出，不能删除。\n");
                    return;
                }
                flag = true;
                continue;
            }
            newDvds[j++] = dvds[i];
        }
        if (!flag) {
            System.out.println("DVD库中没有找到名称为《" + name + "》的DVD。\n");
            return;
        }
        dvds = newDvds;

        System.out.println("成功删除DVD《" + name + "》。\n");
    }



    // 借出DVD
    public void rentDvd() {
        if (dvds.length == 0) {
            System.out.println("DVD库中还没有DVD。\n");
            return;
        }

        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入DVD名称：");
        String name = scanner.nextLine();

        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i].getTitle().equals(name)) {
                if (!dvds[i].isAvailable()) {
                    System.out.println("DVD《" + dvds[i].getTitle() + "》已经被借出了。\n");
                    return;
                }
                flag = true;

                System.out.print("请输入借出天数：");
                int days = scanner.nextInt();

                dvds[i].setAvailable(false);
                dvds[i].setRented(true);
                dvds[i].setRentedDate(LocalDate.now());
                dvds[i].setDays(days);

                System.out.println("成功借出DVD《" + dvds[i].getTitle() + "》，借出日期为：" + dvds[i].getRentedDate().toString() + "，借出天数为：" + days + "天。\n");
                break;
            }
        }
        if (!flag) {
            System.out.println("DVD库中没有找到名称为《" + name + "》的DVD。\n");
        }
    }

    // 归还DVD

    public void returnDvd() {
        if (dvds.length == 0) {
            System.out.println("DVD库中还没有DVD。\n");
            return;
        }

        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入DVD名称：");
        String name = scanner.nextLine();

        boolean flag = false;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i].getTitle().equals(name)) {
                if (dvds[i].isAvailable()) {
                    System.out.println("DVD《" + dvds[i].getTitle() + "》没有被借出。\n");
                    return;
                }

                LocalDate rentedDate = dvds[i].getRentedDate();
                LocalDate now = LocalDate.now();
                int rentedDays = rentedDate.until(now).getDays();
                int rentalFee = dvds[i].getRentalFee() * dvds[i].getDays();
                int lateFee = 0;

                if (rentedDays > dvds[i].getDays()) {
                    lateFee = (rentedDays - dvds[i].getDays()) * dvds[i].getLateFee();
                }

                int totalFee = rentalFee + lateFee;
                int deposit = dvds[i].getDeposit();

                System.out.println("DVD《" + dvds[i].getTitle() + "》借出日期为：" + rentedDate.toString() + "，借出天数为：" + dvds[i].getDays() + "天，应付租金为：" + rentalFee + "元，应付补费为：" + lateFee + "元，已付押金为：" + deposit + "元，应找零：" + (deposit - totalFee) + "元。\n");

                dvds[i].setAvailable(true);
                dvds[i].setRented(false);
                dvds[i].setRentedDate(null);
                dvds[i].setDays(0);

                flag = true;
                break;
            }
        }
        if (!flag) {
            System.out.println("DVD库中没有找到名称为《" + name + "》的DVD。\n");
        }
    }



}